part of '../repository.dart';

class _TransactionDtoFactory {
  const _TransactionDtoFactory._internal();

  static const _instance = _TransactionDtoFactory._internal();

  factory _TransactionDtoFactory() {
    return _instance;
  }

  _TransactionDto fromJson(Map<String, dynamic> json) {
    final purchaseType = json['type'];

    if (purchaseType == _kPurchasedSubscriptionTransactionTypeValue) {
      return _SubscriptionTransactionDto.fromJson(json);
    }

    if (purchaseType == _kOneTimePurchasedItemTransactionTypeValue) {
      return _OneTimePurchasedItemTransactionDto.fromJson(json);
    }

    throw UnimplementedError();
  }

  _TransactionDto fromEntity(AppStoreTransaction transaction) {
    if (transaction is AppStorePurchasedSubscriptionTransaction) {
      return _SubscriptionTransactionDto.fromEntity(transaction);
    }

    if (transaction is AppStoreOneTimePurchasedItemTransaction) {
      return _OneTimePurchasedItemTransactionDto.fromEntity(transaction);
    }

    throw UnimplementedError();
  }
}
